<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of departamento
 *
 * @author konda
 */
class departamento extends CI_Controller {
    //put your code here
    
    public function __construct() {
        parent::__construct();
        
         //library
         $this->load->library(array('table','pagination','form_validation'));
         
         //helper
         $this->load->helper(array('form','url'));
         //model
         $this->load->model('m_departamento');
         $this->load->model('m_generic');

         //initialize los valores 
         //parameters
         $dataInitialize=array();
         $dataInitialize['table']='sc_dpto';
         $dataInitialize['id_table']='dpto_id';
         $this->m_generic->initialize($dataInitialize);

   
     }
    
    
    /**
     * Index : load view Department
     */
    public function Index()
    {
         

       if($this->session->userdata('login_state')==TRUE)
       {
         ///////////////////////////////////////////////////////
         //Data
         //title page
         $data['title']="Departamento";
         $data['view']="v_departamento";
         
         $data['menu_header']=  $this->menu_header();
         $data['menu_left']  =  $this->menu_left();
         
         
         $this->load->view('template/header.php',$data);
         $this->load->view('template/content.php',$data);
         $this->load->view('template/footer.php');
             
       }else
       {
        redirect('login');
       }
    }
    
    /**
     * Get list of Departments
     */
    public function getList()
    {    
        $rows=array();
        //accede a getAll
        $rows=$this->m_departamento->getAll();
        $rows_count=$this->m_departamento->getRowCount();
       
        //Return result to jTable
       $jTableResult = array();
       //cabecera header
       $jTableResult['Result']  = "OK";
       $jTableResult['Records'] = $rows;
       $jTableResult['TotalRecordCount']=$rows_count;
       print json_encode($jTableResult);
      
    }
    

    /**
     * Get list Departments Departments 
     */
    public function GetDepartmentOptions()
    {    
        //accede a getAll
        $rows=$this->m_generic->getAll();
        $rows_count=$this->m_generic->getRowCount();
     
        
        //se prepara para los valores 
        
         $options=array();
         foreach ($rows as $row ) {
         
         $options[]=array('DisplayText' =>$row->dpto_nombre , 
                          'Value'=>$row->dpto_id);
        }
        //Return result to jTable
       $jTableResult = array();
       //cabecera header
       $jTableResult['Result']  = "OK";
       $jTableResult['Options'] = $options;
     
       print json_encode($jTableResult);
      
    }

    /**function add**/
    public function add()
    {
       
        $dpto_nombre=$_POST['dpto_nombre'];
        $dpto_descripcion=$_POST['dpto_descripcion'];
             
        $this->form_validation->set_rules('dpto_nombre','nombre','trim|required|xss_clean');
        $this->form_validation->set_rules('dpto_descripcion','descripcion','trim|required|xss_clean');
  
        if($this->form_validation->run()==FALSE)
        {  //some error
            $result['success']=FALSE;
            $result['message']="Ingrese correcto nombre o descripcion";
            $result['error']=  validation_errors();
            echo json_encode($result);
        }
        else 
        {   //correct
            $data=array(
                'dpto_nombre'=>$dpto_nombre,
                'dpto_descripcion'=>$dpto_descripcion
            );
            
            //add
            $this->m_departamento->add($data);
            
            //return value 
            //Return result to jTable
             $jTableResult = array();
             //cabecera header
            $jTableResult['Result']  = "OK";
            $jTableResult['Record'] = $this->m_departamento->getRowLastInsert();
            print json_encode($jTableResult);
            

        }


    }
    
    /**
     * Funcion que devuelve un 
     * objeto a partir de un determinado
     * id
     */
    public function getById()
    {
     $dpto_id=$_POST["id"];
     $data=array();
     $data=$this->m_departamento->getById($dpto_id);
     echo json_encode($data);

    }


    
    /**
     * Function delete data 
     * method POST
     */
    public function delete()
    {
        $dpto_id=$_POST['dpto_id'];
        $this->m_departamento->delete($dpto_id);
        $result=array();
        $result['Result']="OK";
        echo json_encode($result);
        
    }
    


    public function update()
    {
      
        $dpto_id=$_POST['dpto_id'];
        $dpto_nombre=$_POST['dpto_nombre'];
        $dpto_descripcion=$_POST['dpto_descripcion'];
        
        //validation rules
        $this->form_validation->set_rules('dpto_nombre','nombre','trim|required|xss_clean');
        $this->form_validation->set_rules('dpto_descripcion','descripcion','trim|required|xss_clean');
   
        if($this->form_validation->run()==FALSE)
        {
            //some error
            $result['success']=FALSE;
            $result['message']="Ingrese correcto nombre o descripcion";
            $result['error']=  validation_errors();
            echo json_encode($result);
             
        }
        else
        {
            //data
            $data=array(
                'dpto_id'=>$dpto_id,
                'dpto_nombre'=>$dpto_nombre,
                'dpto_descripcion'=>$dpto_descripcion
                
            );
            
            //Update Data 
            $this->m_departamento->update($dpto_id,$data);
           
            //Return result to jTable
            $jTableResult = array();
            $jTableResult['Result'] = "OK";
            print json_encode($jTableResult);
        }
        
    }
    
    
     /**
     * Functions of variables
     * 
     */
    
      public function menu_header()
    {



       $menu_header="<div class=\"navbar navbar-fixed-top navbar-inverse\">";
       $menu_header.="<div class=\"navbar-inner\">";
       $menu_header.="<ul class=\"nav\">";
       $menu_header.="<li class=\"active\">";
       $menu_header.=anchor("inicio","Inicio");
       $menu_header.="</li>";

       $menu_header.="<li class=\"divider-vertical\"></li>";
       $menu_header.="<li>";
       $menu_header.=anchor("map","Mapas");
       $menu_header.="</li>";
       $menu_header.="</ul>";
       $menu_header.="<ul class=\"nav pull-right\">";
       $menu_header.="<li>";
       $menu_header.="<a href=\"#\">";
       $menu_header.="Usuario1";
       $menu_header.="</a>";
       $menu_header.="</li>";
       $menu_header.="<li class=\"divider-vertical\"></li>";
       $menu_header.="<li class=\"dropdown\" data-no-collapse=\"true\">";
       $menu_header.="<a class=\"dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">";
       $menu_header.="Configuration<b class=\"caret\"></b>";
       $menu_header.="</a>";
       
       $menu_header.="<ul class=\"dropdown-menu\">";
       $menu_header.="<li>";
       $menu_header.=anchor("logout","Salir");
       $menu_header.="</li>";
       $menu_header.="</ul>";
       
       $menu_header.="</li>"; //class dropdown
       $menu_header.="</ul>"; //class nav pull right
       $menu_header.="</div>";
       $menu_header.="</div>";

       //black header
       //$menu_header.="<div class=\"gbx1\"></div>"; 
       
       return $menu_header;
    }
    
    
    
   
    public function menu_left()
    {
        $menu_left="<div id=\"leftCol\">";
        $menu_left.="<ul id=\"menu\">";
        $menu_left.="<li>";
        $menu_left.=anchor("proyecto","Proyecto");
        $menu_left.="</li>";
        $menu_left.="<li class=\"ui-state-highlight\">";
        $menu_left.=anchor("departamento","Departamento");
        $menu_left.="</li>";
        $menu_left.="<li>";
        $menu_left.=anchor("localidad","Localidad");
        $menu_left.="</li>";
        $menu_left.="<li>";
        $menu_left.=anchor("municipio","Municipio");
        $menu_left.="</li>";
        $menu_left.="<li>";
        $menu_left.=anchor("obra","Obras");
        $menu_left.="</li>";
        $menu_left.="</ul>";
        $menu_left.="</div>";  
        
        return $menu_left;
              
    }

}

?>
